package com.czy.owner.widget.timersection;

import android.content.Context;
import android.view.View;

import com.czy.owner.R;
import com.jzxiang.pickerview.adapters.NumericWheelAdapter;
import com.jzxiang.pickerview.config.PickerConfig;
import com.jzxiang.pickerview.utils.PickerContants;
import com.jzxiang.pickerview.utils.Utils;
import com.jzxiang.pickerview.wheel.OnWheelChangedListener;
import com.jzxiang.pickerview.wheel.WheelView;

import java.util.Calendar;

/**
 * Created by Raven on 2018/4/9.
 */

public class SectionWheel {
    Context mContext;
    WheelView s_year, s_month, s_day;//开始日期 年月日
    WheelView e_year, e_month, e_day;//结束日期 年月日

    NumericWheelAdapter mStartYearAdapter, mStartMonthAdapter, mStartDayAdapter;
    NumericWheelAdapter mEndYearAdapter, mEndMonthAdapter, mEndDayAdapter;
    PickerConfig mPickerConfig;
    SectionTimeRepository mRepository;

    OnWheelChangedListener startYearListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateStartMonths();
        }
    };
    OnWheelChangedListener startMonthListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateStartDays();
        }
    };

    OnWheelChangedListener endYearListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateEndMonths();
        }
    };
    OnWheelChangedListener endMonthListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView wheel, int oldValue, int newValue) {
            updateEndDays();
        }
    };


    public SectionWheel(View view, PickerConfig sectionConfig) {
        mPickerConfig = sectionConfig;

        mRepository = new SectionTimeRepository(sectionConfig);
        mContext = view.getContext();
        initialize(view);
    }

    public void initialize(View view) {
        initView(view);
        initStartYear();
        initStartMonth();
        initStartDay();

        initEndYear();
        initEndMonth();
        initEndDay();

    }

    void initView(View view) {
        s_year = (WheelView) view.findViewById(R.id.start_year);
        s_month = (WheelView) view.findViewById(R.id.start_month);
        s_day = (WheelView) view.findViewById(R.id.start_day);

        e_year = (WheelView) view.findViewById(R.id.end_year);
        e_month = (WheelView) view.findViewById(R.id.end_month);
        e_day = (WheelView) view.findViewById(R.id.end_day);

        switch (mPickerConfig.mType) {
            case YEAR_MONTH_DAY:

                break;
            case YEAR_MONTH:
                Utils.hideViews(s_day,e_day);
                break;
        }

        s_year.addChangingListener(startYearListener);
        s_year.addChangingListener(startMonthListener);
        s_month.addChangingListener(startMonthListener);

        e_year.addChangingListener(endYearListener);
        e_year.addChangingListener(endMonthListener);
        e_month.addChangingListener(endMonthListener);


    }

    void initStartYear() {
        int minYear = mRepository.getMinYear();
        int maxYear = mRepository.getMaxYear();

        mStartYearAdapter = new NumericWheelAdapter(mContext, minYear, maxYear, PickerContants.FORMAT, mPickerConfig.mYear);
        mStartYearAdapter.setConfig(mPickerConfig);
        s_year.setViewAdapter(mStartYearAdapter);
        s_year.setCurrentItem(mRepository.getDefaultStartCalendar().year - minYear);
    }

    void initEndYear() {
        int minYear = mRepository.getMinYear();
        int maxYear = mRepository.getMaxYear();

        mStartYearAdapter = new NumericWheelAdapter(mContext, minYear, maxYear, PickerContants.FORMAT, mPickerConfig.mYear);
        mStartYearAdapter.setConfig(mPickerConfig);
        e_year.setViewAdapter(mStartYearAdapter);
        e_year.setCurrentItem(mRepository.getDefaultEndCalendar().year - minYear);
    }

    void initStartMonth() {
        updateStartMonths();
        int curYear = getCurrentStartYear();
        int minMonth = mRepository.getMinMonth(curYear);
        s_month.setCurrentItem(mRepository.getDefaultStartCalendar().month - minMonth);
        s_month.setCyclic(mPickerConfig.cyclic);
    }

    void initEndMonth() {
        updateEndMonths();
        int curYear = getCurrentEndYear();
        int minMonth = mRepository.getMinMonth(curYear);
        e_month.setCurrentItem(mRepository.getDefaultEndCalendar().month - minMonth);
        e_month.setCyclic(mPickerConfig.cyclic);
    }

    void initStartDay() {
        updateStartDays();
        int curYear = getCurrentStartYear();
        int curMonth = getCurrentStartMonth();

        int minDay = mRepository.getMinDay(curYear, curMonth);
        s_day.setCurrentItem(mRepository.getDefaultStartCalendar().day - minDay);
        s_day.setCyclic(mPickerConfig.cyclic);
    }

    void initEndDay() {
        updateEndDays();
        int curYear = getCurrentEndYear();
        int curMonth = getCurrentEndMonth();

        int minDay = mRepository.getMinDay(curYear, curMonth);
        e_day.setCurrentItem(mRepository.getDefaultEndCalendar().day - minDay);
        e_day.setCyclic(mPickerConfig.cyclic);
    }

    void updateStartMonths() {
        if (s_month.getVisibility() == View.GONE)
            return;

        int curYear = getCurrentStartYear();
        int minMonth = mRepository.getMinMonth(curYear);
        int maxMonth = mRepository.getMaxMonth(curYear);
        mStartMonthAdapter = new NumericWheelAdapter(mContext, minMonth, maxMonth, PickerContants.FORMAT, mPickerConfig.mMonth);
        mStartMonthAdapter.setConfig(mPickerConfig);
        s_month.setViewAdapter(mStartMonthAdapter);

        if (mRepository.isMinYear(curYear)) {
            s_month.setCurrentItem(0, false);
        }
    }

    void updateEndMonths() {
        if (e_month.getVisibility() == View.GONE)
            return;

        int curYear = getCurrentStartYear();
        int minMonth = mRepository.getMinMonth(curYear);
        int maxMonth = mRepository.getMaxMonth(curYear);
        mEndMonthAdapter = new NumericWheelAdapter(mContext, minMonth, maxMonth, PickerContants.FORMAT, mPickerConfig.mMonth);
        mEndMonthAdapter.setConfig(mPickerConfig);
        e_month.setViewAdapter(mEndMonthAdapter);

        if (mRepository.isMinYear(curYear)) {
            e_month.setCurrentItem(0, false);
        }
    }

    void updateStartDays() {
        if (s_day.getVisibility() == View.GONE)
            return;

        int curYear = getCurrentStartYear();
        int curMonth = getCurrentStartMonth();

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + s_year.getCurrentItem());
        calendar.set(Calendar.MONTH, curMonth);

        int maxDay = mRepository.getMaxDay(curYear, curMonth);
        int minDay = mRepository.getMinDay(curYear, curMonth);
        mStartDayAdapter = new NumericWheelAdapter(mContext, minDay, maxDay, PickerContants.FORMAT, mPickerConfig.mDay);
        mStartDayAdapter.setConfig(mPickerConfig);
        s_day.setViewAdapter(mStartDayAdapter);

        if (mRepository.isMinMonth(curYear, curMonth)) {
            s_day.setCurrentItem(0, true);
        }

        int dayCount = mStartDayAdapter.getItemsCount();
        if (s_day.getCurrentItem() >= dayCount) {
            s_day.setCurrentItem(dayCount - 1, true);
        }
    }

    void updateEndDays() {
        if (e_day.getVisibility() == View.GONE)
            return;

        int curYear = getCurrentEndYear();
        int curMonth = getCurrentEndMonth();

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + e_year.getCurrentItem());
        calendar.set(Calendar.MONTH, curMonth);

        int maxDay = mRepository.getMaxDay(curYear, curMonth);
        int minDay = mRepository.getMinDay(curYear, curMonth);
        mEndDayAdapter = new NumericWheelAdapter(mContext, minDay, maxDay, PickerContants.FORMAT, mPickerConfig.mDay);
        mEndDayAdapter.setConfig(mPickerConfig);
        e_day.setViewAdapter(mEndDayAdapter);

        if (mRepository.isMinMonth(curYear, curMonth)) {
            e_day.setCurrentItem(0, true);
        }

        int dayCount = mEndDayAdapter.getItemsCount();
        if (e_day.getCurrentItem() >= dayCount) {
            e_day.setCurrentItem(dayCount - 1, true);
        }
    }


    public int getCurrentStartYear() {
        return s_year.getCurrentItem() + mRepository.getMinYear();
    }

    public int getCurrentEndYear() {
        return e_year.getCurrentItem() + mRepository.getMinYear();
    }

    public int getCurrentStartMonth() {
        int curYear = getCurrentStartYear();
        return s_month.getCurrentItem() + +mRepository.getMinMonth(curYear);
    }

    public int getCurrentEndMonth() {
        int curYear = getCurrentEndYear();
        return e_month.getCurrentItem() + +mRepository.getMinMonth(curYear);
    }

    public int getCurrentStartDay() {
        int curYear = getCurrentStartYear();
        int curMonth = getCurrentStartMonth();
        return s_day.getCurrentItem() + mRepository.getMinDay(curYear, curMonth);
    }

    public int getCurrentEndDay() {
        int curYear = getCurrentStartYear();
        int curMonth = getCurrentStartMonth();
        return e_day.getCurrentItem() + mRepository.getMinDay(curYear, curMonth);
    }

}
